﻿using Company.Database.Base.Entities;
using Company.Database.Base.Repositories;

namespace Company.Database.SqlServer.Repositories
{
    class StationRepository : IStationRepository
    {
        private readonly SqlServerDbContext _context;
        public StationRepository(SqlServerDbContext context)
        {
            _context = context;
        }

        public async Task<int> DeleteAsync(StationEntity entity)
        {
            return await Task.Run(() =>
            {
                _context.Entry(entity).State = Microsoft.EntityFrameworkCore.EntityState.Deleted;
                return _context.SaveChanges();
            });
        }

        public async Task<List<StationEntity>> GetAllAsync()
        {
            return await Task.Run(() =>
            {
                return _context.Stations.ToList();
            });
        }

        public async Task<StationEntity> GetByIdAsync(int id)
        {
            return await _context.Stations.FindAsync(id) ?? new StationEntity();
        }

        public async Task<int> InsertAsync(StationEntity entity)
        {
            await _context.Stations.AddAsync(entity);
            return await _context.SaveChangesAsync();
        }

        public async Task<int> UpdateAsync(StationEntity entity)
        {
            return await Task.Run(() =>
            {
                _context.Entry(entity).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                return _context.SaveChanges();
            });
        }
    }
}
